app = {} //The app table is deleted later, so store data in app.app._ = {} instead. Not using the table app.app._ in specific will cause problems.
app.folder = {"Utilities"} //This table should have one key and one value, key of one, value of a string. This is for FUTURE subfolder support.
app.app = {
	caption = "Calculator",
	options = {
			close = true,
			minimize = true,
		},
	x = ScrW()*.5 - 128 + 256 + 16,
	y = ScrH()*.5 - 64,
	w = 120,
	h = 176,
	_ = {
		num = nil,
		cur = nil,
		op = nil,
	},
	draw = function(self)
		if not self.visible then
			return
		end
		local s = hack.scheme
		self:DrawWindowPre()
		
		self:DrawRoundedRect(12,12,self.w-24,12,4,Color(255,255,255,255),true)
		
		local disp = tostring(self._.cur or self._.num or "")
		if #disp > 9 then
			disp = "Error"
			self._.cur = math.Round((tonumber(self._.cur) or 0)*100)
			if self._.cur ~= 0 then
				self._.cur = self._.cur/100
			end
		end
		self:DrawText(disp,"Trebuchet24",self.w-12,6,Color(0,0,0,255),2)
		
		local button = {
			w = 24,
			h = 24,
			font = "Trebuchet24",
		}
		
		local i = 0
		local numIn
		for y = 52,0,-26 do
			button.y = y+38+26
			for x = 0,52,26 do
				i = i+1
				button.x = x+8
				button.label = i
				local newIn = self:DrawForm(i,"button",button)
				if newIn == -1 then
					newIn = i
				else
					newIn = nil
				end
				numIn = numIn or newIn
			end
		end
		
		local opIn
		do
			button.label = "C"
			button.x,button.y = 8,38
			local newIn = self:DrawForm(button.label,"button",button)
			if newIn == -1 then
				newIn = "C"
			else
				newIn = nil
			end
			opIn = opIn or newIn
			
			button.label = "CE"
			button.x,button.y = 8+26,38
			local newIn = self:DrawForm(button.label,"button",button)
			if newIn == -1 then
				newIn = "CE"
			else
				newIn = nil
			end
			opIn = opIn or newIn
			
			button.label = "/"
			button.x,button.y = 8+26*2,38
			local newIn = self:DrawForm(button.label,"button",button)
			if newIn == -1 then
				newIn = "/"
			else
				newIn = nil
			end
			opIn = opIn or newIn
			
			button.label = "*"
			button.x,button.y = 8+26*3,38
			local newIn = self:DrawForm(button.label,"button",button)
			if newIn == -1 then
				newIn = "*"
			else
				newIn = nil
			end
			opIn = opIn or newIn
			
			button.label = "-"
			button.x,button.y = 8+26*3,38+26
			local newIn = self:DrawForm(button.label,"button",button)
			if newIn == -1 then
				newIn = "-"
			else
				newIn = nil
			end
			opIn = opIn or newIn
			
			button.label = "+"
			button.x,button.y = 8+26*3,38+26*2
			local newIn = self:DrawForm(button.label,"button",button)
			if newIn == -1 then
				newIn = "+"
			else
				newIn = nil
			end
			opIn = opIn or newIn
			
			button.label = "="
			button.x,button.y = 8+26*3,38+26*3
			button.h = 48+2
			local newIn = self:DrawForm(button.label,"button",button)
			button.h = 24
			if newIn == -1 then
				newIn = "="
			else
				newIn = nil
			end
			opIn = opIn or newIn
			
			button.label = "."
			button.x,button.y = 8+26*2,38+26*4
			local newIn = self:DrawForm(button.label,"button",button)
			if newIn == -1 then
				newIn = "."
			else
				newIn = nil
			end
			numIn = numIn or newIn
			
			button.label = 0
			button.x,button.y = 8+26,38+26*4
			local newIn = self:DrawForm(button.label,"button",button)
			if newIn == -1 then
				newIn = 0
			else
				newIn = nil
			end
			numIn = numIn or newIn
			
			button.label = "+-"
			button.x,button.y = 8,38+26*4
			local newIn = self:DrawForm(button.label,"button",button)
			if newIn == -1 then
				newIn = "+-"
			else
				newIn = nil
			end
			opIn = opIn or newIn
			
			//Perform calculations
			
			if numIn then
				if self._.op == nil and self._.lop then
					self._.num = nil
					self._.cur = nil
					self._.op = nil
					self._.lop = nil
				end
				self._.cur = (self._.cur or "")..numIn
			end
			
			if opIn then
				if tonumber(self._.cur) then
					if self._.op and
						self._.cur and
						self._.num and
						(
							opIn == "/" or
							opIn == "*" or
							opIn == "-" or
							opIn == "+"
						)
					then
						if self._.op == "/" then
							if self._.cur == "0" then
								self._.cur = "1"
							end
							self._.lop = {self._.op,self._.cur}
							self._.cur = self._.num/self._.cur
							self._.op = nil
						elseif self._.op == "*" then
							self._.lop = {self._.op,self._.cur}
							self._.cur = self._.num*self._.cur
							self._.op = nil
						elseif self._.op == "-" then
							self._.lop = {self._.op,self._.cur}
							self._.cur = self._.num-self._.cur
							self._.op = nil
						elseif self._.op == "+" then
							self._.lop = {self._.op,self._.cur}
							self._.cur = self._.num+self._.cur
							self._.op = nil
						end
					end
					if opIn == "=" then
						if self._.op then
							if self._.op == "/" then
								if self._.cur == "0" then
									self._.cur = "1"
								end
								self._.lop = {self._.op,self._.cur}
								self._.cur = self._.num/self._.cur
								self._.op = nil
							elseif self._.op == "*" then
								self._.lop = {self._.op,self._.cur}
								self._.cur = self._.num*self._.cur
								self._.op = nil
							elseif self._.op == "-" then
								self._.lop = {self._.op,self._.cur}
								self._.cur = self._.num-self._.cur
								self._.op = nil
							elseif self._.op == "+" then
								self._.lop = {self._.op,self._.cur}
								self._.cur = self._.num+self._.cur
								self._.op = nil
							end
						elseif self._.lop then
							if self._.lop[1] == "/" then
								self._.cur = self._.cur/self._.lop[2]
							elseif self._.lop[1] == "*" then
								self._.cur = self._.cur*self._.lop[2]
							elseif self._.lop[1] == "-" then
								self._.cur = self._.cur-self._.lop[2]
							elseif self._.lop[1] == "+" then
								self._.cur = self._.cur+self._.lop[2]
							end
						end
					end
					if opIn == "C" then
						self._.num = nil
						self._.cur = nil
						self._.op = nil
						self._.lop = nil
					elseif opIn == "CE" then
						self._.cur = nil
						self._.lop = nil
					elseif opIn == "/" or opIn == "*" or opIn == "-" or opIn == "+" then
						self._.num = self._.cur
						self._.cur = nil
						self._.op = opIn
						self._.lop = nil
					end
					if opIn == "+-" then
						self._.cur = -1 * self._.cur
					end
				else
					self._.cur = nil
				end
			end
		end
		
		self:DrawWindowPost()
	end,
}
























































